package com.javafactroy.kunlong.data.dto;

import com.javafactroy.kunlong.data.convert.QueryConditionParamConvert;
import com.javafactroy.kunlong.domain.generalquery.valueobj.QueryConditionParam;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import com.javafactroy.kunlong.data.entity.QueryConditionApiEntity;
import lombok.experimental.Accessors;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;

import javax.persistence.*;
import java.util.Optional;

/**
 * @author raymond.lei@163.com
 * @date 2023-11-30 16:54:03
 */
@Data
@Table(name = "query_condition")
@ApiModel("")
@Accessors(chain = true)
@ExcelIgnoreUnannotated
public class QueryConditionApiDto extends QueryConditionApiEntity {

    @Transient
    protected Boolean paramFlag;

    Boolean whenBetween(){
        return getQueryType().contains("between");
    }
    Boolean whenLike(){
        return getQueryType().contains("like");
    }
    Boolean whenIn(){
        return getQueryType().contains("in");
    }
    public Optional<QueryConditionParam> parseParam(){

        if(whenBetween()){
            if(getStartValue().contains("@")){
                paramFlag = true;
                return Optional.of(QueryConditionParamConvert.INSTANCE.toOne(this));
            }
        }
        if(whenLike()){
            if(getLikeValue().contains("@")){
                paramFlag = true;
                return Optional.of(QueryConditionParamConvert.INSTANCE.toOne(this));
            }
        }
        if(whenIn()){
            if(getInValue().contains("@")){
                paramFlag = true;
                return Optional.of(QueryConditionParamConvert.INSTANCE.toOne(this));
            }
        }
        if(getEqValue().contains("@")){
            paramFlag = true;
            return Optional.of(QueryConditionParamConvert.INSTANCE.toOne(this));
        }
        return Optional.empty();
    }
    // in like between
    // not in , not like, not between
    // = !=  < > >= <=
    public StringBuilder toWhere() {
        StringBuilder stringBuilder = new StringBuilder(256);
        stringBuilder.append(getFieldName()).append(" ").append(getQueryType()).append(" ");

        if (getQueryType().contains("between")) {
            toBetween(stringBuilder);
        } else if (getQueryType().contains("in")) {
            toIn(stringBuilder);
        } else if (getQueryType().contains("like")) {
            toLike(stringBuilder);
        }else{
            toOther(stringBuilder);
        }

        return stringBuilder;
    }
    public Boolean isStr(){
        return getFieldType().equals("varchar")||getFieldType().equals("char");
    }

    QueryConditionApiDto toBetween(StringBuilder stringBuilder) {
        if (getQueryType().contains("between")) {
            if (getFieldType().equalsIgnoreCase("date")) {
                stringBuilder.append(CommonUtils.quatStr(getStartValue())).append(" and ").append(CommonUtils.quatStr(getEndValue()));

            } else if (getFieldType().equalsIgnoreCase("datetime")) {
                stringBuilder.append(CommonUtils.quatStr(getStartValue())).append(" and ").append(CommonUtils.quatStr(getEndValue()));

            } else if (getFieldType().contains("char")) {
                stringBuilder.append(CommonUtils.quatStr(getStartValue())).append(" and ").append(CommonUtils.quatStr(getEndValue()));

            } else {
                stringBuilder.append(getStartValue()).append(" and ").append(getEndValue());
            }
        }
        return this;
    }

    QueryConditionApiDto toLike(StringBuilder stringBuilder) {
        if (getQueryType().contains("like")) {
            {
                stringBuilder.append(" ").append("%"+getLikeValue()+"%");
            }
        }
        return this;
    }

    QueryConditionApiDto toIn(StringBuilder stringBuilder) {
        if (getQueryType().contains("in")) {
          stringBuilder.append(" in (").append(getInValue()).append(")");

        }
        return this;
    }
    QueryConditionApiDto toOther(StringBuilder stringBuilder) {

            if (getFieldType().contains("char")) {
                stringBuilder.append(" ").append(CommonUtils.quatStr(getEqValue()));

            }   else {
                stringBuilder.append(" ").append(getEqValue());
            }

        return this;
    }
}